package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //获取手机号验证码
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
       //获取手机号 随机生成6位的验证码 语句输出打印
        //获取用户的手机号
        String phone = user.getPhone();
        //判断用户手机号是否为空
        if(StringUtils.isNotEmpty(phone)){
            //使用ValidateCodeUtils工具类随机生成6位验证码
            String code = ValidateCodeUtils.generateValidateCode4String(6);
            //从控制台来获取到code 6位验证码
            log.info("code={}",code);
            //使用SMSUtils工具类来发送短信
//            SMSUtils.sendMessage("瑞吉外卖","",phone,code1);
            //需要将生成的验证码存储到session中
//            session.setAttribute(phone,code);
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            return R.success("短信验证码发送成功！！！");
        }
        return R.error("短信验证码发送失败!!!");
    }




    //user登录
    @PostMapping("/login")
    public R<String> login(@RequestBody Map map, HttpSession session){
        log.info(map.toString());
        //map组合用k=phone v=code 利用map来接收
        //写入的手机号与验证码是否对应相等
        //输入的手机号与验证码
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        //从之前的session获取正确的验证码,这里的sessioncode已是string类型
//        Object sessioncode =  session.getAttribute(phone);
        Object sessioncode = redisTemplate.opsForValue().get(phone);
        //获取的验证码不为空且正确验证码与输入的验证码相等的情况下 登录成功
        if(sessioncode!=null&&sessioncode.equals(code)){
            //如果phone不在user.phone中,如果user不等于null就创建新user
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(lambdaQueryWrapper);
            if(user==null){
                //新创建一个user
                User user1 = new User();
                user1.setPhone(phone);
                user1.setStatus(1);
                //添加到user表中
                userService.save(user1);
                //            将登录用户的ID存储Session中
                session.setAttribute("user",user1.getId());
                redisTemplate.delete(phone);
            }else {
                session.setAttribute("user",user.getId());
                redisTemplate.delete(phone);
            }
            return R.success("登录成功！！！");
        }
        //判断后台传入的验证码与页面你写的验证码判断是否相等
        return R.error("登录失败！！！");
    }

    //退出登录
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession session){
        session.removeAttribute("user");
        return R.success("退出成功！！!");
    }

}
